<?php
namespace app\index\controller;

use extend\Upload;
use extend\Verify;
use lib\Response;
use lib\Session;
use lib\Db;

class Member extends Common {

    /**
     * 登录
     */
    function login(){
        return $this->fetch();
    }

    /**
     * 注册
     */
    function register(){
        return $this->fetch();
    }

    /**
     * 个人中心
     */
    function center(){
        $uid = $this->data['uid'] ? $this->data['uid'] : Session::get('uid');
        if(!is_numeric($uid)){
            return $this->error('获取uid失败!');
        }
        $info = Db::name('user u')->field(['u.id', 'u.nickname', 'u.avatar', 'u.time', 'i.mobile'
            , 'i.wechat', 'i.qq', 'i.age', 'i.sex'])
            ->join('user_info i', 'u.id = i.uid')
            ->where(['u.id'=>$uid])
	        ->find();
        $this->set('info', $info);
        return $this->fetch();
    }

    /**
     * 修改个人信息
     */
    function modify(){
        $uid = Session::get('uid');
        if(!is_numeric($uid)){
            $this->error('获取uid失败!');
        }
        $info = Db::name('user_info')->where(['uid'=>$uid])->find();
        $this->set('info', $info);
        return $this->fetch();
    }

    /**
     * 退出登陆
     */
    function logout(){
        Session::remove('user_info');
        Session::remove('uid');
        $this->success('操作成功');
    }

    //====================Ajax

    /**
     * 登录动作
     * @return array
     */
    function _login(){
        $params = $this->request->post;
        params_check(array('account', 'password', 'code'));
        if(!Verify::check($params['code'])){
            return Response::error('验证码输入错误!');
        }
        $result = Db::name('user')->where(['account'=>$params['account']])->find();
        if($result){
            if(password_verify($params['password'], $result['password'])){
            	unset($result['password']);
                Session::set('user_info', $result);
                Session::set('uid', $result['id']);
                return Response::success();
            }else{
                return Response::error('密码不正确!');
            }
        }else{
            return Response::error('没有该用户!');
        }
    }

    /**
     * 注册动作
     * @return array
     */
    function _register(){
        $params = $this->request->post;
        params_check(array('account', 'nickname', 'password', 'code'), $this->request->post);
        if(!Verify::check($params['code'])){
            return Response::error('验证码输入错误!');
        }
        if(Db::name('user')->where(array('account'=>$params['account']))->find()){
            return Response::error('该用户已经存在!');
        }
        $user_base = array(
            'account' => $params['account'],
            'nickname' => $params['nickname'],
            'password' => password_hash($params['password'], PASSWORD_DEFAULT),
            'time'  => date('Y-m-d H:i:s')
        );
        if($insert_id = Db::name('user')->insert($user_base)){
            if(Db::name('user_info')->insert(array('uid'=>$insert_id))){
                $user_base['uid'] = $insert_id;
                Session::set('user_info', $user_base);
                Session::set('uid', $insert_id);
                return Response::success();
            }else{
                return Response::error("写入用户信息失败!");
            }
        }else{
            return Response::error("创建用户失败!");
        }
    }

	/**
	 * 改变头像
	 * @return array
	 */
    function _changeAvatar(){
    	$uid = $this->request->post('uid');
    	if(!$uid){
		    return Response::error("获取用户id失败");
	    }
	    if($this->request->file('avatar')){
		    $upload = new Upload(['avatar']);
		    $res = $upload->move();
		    $result = Db::name('user')->where(['id'=>$uid])->update(['avatar'=> $res['avatar']['relative']]);
		    return Response::success('ok', $result);
	    }
	    return Response::error("请上传文件");
    }

    /**
     * 修改用户信息
     */
    function _modify(){
        $params = $this->request->post;
        params_check(array('name', 'age', 'birthday', 'address', 'star', 'sex', 'mood'));
        if(Db::name('user_info')->where(['id'=>$params['uid']])->update(array(
            'sex'   => $params['sex'],
            'age'   => $params['age'],
            'star'  => $params['star'],
            'name'  => $params['name'],
            'mood'  => $params['mood'],
            'birthday'  => $params['birthday'],
            'address'   => $params['address'],
        ))){
            return Response::success('success', 1);
        }else{
            return Response::error("创建用户失败!");
        }
    }

}